﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Archiver
{
    internal class BinTree
    {
        public struct XY
        {
            public int x;
            public int y;
        }

        public struct BinNode
        {
            public string Next0;
            public string Next1;
            public XY Value;
        }


        private List<BinNode> Data;

        public BinTree(IEnumerable<FunctionTableValues.TableValues> dList)
        {
            Data = new List<BinNode>();
            var bn = new BinNode {Next0 = "", Value = new XY {x = 0, y = 0}, Next1 = ""};
            Data.Add(bn);
           // bn = new BinNode { Next0 = "", Value = new XY { x = 0, y = 0 }, Next1 = "" };
            //Data.Add(bn);
            
            foreach (var t in dList)
            {
                var t1 = t;
                var j = 0;

                for (var i = 1; i < t1.Value.Count; i++)
                {
                    var u = "";

                    if (t1.Value[i])
                    {
                        u = Data[j].Next1;
                        if (string.IsNullOrEmpty(u))
                        {
                            bn = Data[j];
                            bn.Next1 = (Data.Count).ToString();
                            Data[j] = bn;
                            Data.Add(new BinNode());
                            u = (Data.Count-1).ToString();
                        }
                        j = int.Parse(u);
                        continue;
                    }

                    u = Data[j].Next0;
                    if (string.IsNullOrEmpty(u))
                    {
                        bn = Data[j];
                        bn.Next0 = (Data.Count).ToString();
                        Data[j] = bn;
                        Data.Add(new BinNode());
                        u = (Data.Count-1).ToString();
                    }
                    j = int.Parse(u);
                }
                bn = Data[j];
                bn.Value = new XY {x = t1.X, y = t1.Y};
                Data[j] = bn;
            }
        }

        public bool FindXYByValue(List<bool> a, out XY res)
        {
            var j = 0;
            var u = "";
            for (var i=1; i<a.Count; i++)
            {
                if (a[i])
                    u = Data[j].Next1;
                else
                    u = Data[j].Next0; 
                
                if (string.IsNullOrEmpty(u))
                {
                    res = new XY();
                    return false;
                }
                j = int.Parse(u);
            }
            var t = Data[j];
            if (t.Value.x == t.Value.y && t.Value.x == 0)
            {
                res = new XY();
                return false;
            }
            res = t.Value;
            return true;
        }
    }

}
